package com.edgclub.edg.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.UserInfoOtherActivity;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.RewardUser;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;

import java.util.ArrayList;


public class RewardAdapter extends BaseAdapter {

	private Context mContext;

	private ArrayList<RewardUser> rewardUsers;

	private GlideUtils glideUtils;

	private onAddRewardClickListener addRewardClickListener;
	public RewardAdapter(Context context) {
		this.mContext = context;
		glideUtils = new GlideUtils();
	}

	public onAddRewardClickListener getAddRewardClickListener() {
		return addRewardClickListener;
	}

	public void setAddRewardClickListener(onAddRewardClickListener addRewardClickListener) {
		this.addRewardClickListener = addRewardClickListener;
	}

	public ArrayList<RewardUser> getRewardUsers() {
		return rewardUsers;
	}

	public void setRewardUsers(ArrayList<RewardUser> rewardUsers) {
		this.rewardUsers = rewardUsers;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (null == rewardUsers) {
			return 0;
		}
		if(rewardUsers.size()<5){
			return rewardUsers.size()+1;
		}
		return Math.min(5, rewardUsers.size());
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup group) {
		holder holder;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_reward_user, null);

			holder = new holder();
			holder.image = (ImageView) convertView.findViewById(R.id.image);
			holder.vip = (ImageView) convertView.findViewById(R.id.vip);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_gold_num = (TextView) convertView.findViewById(R.id.tv_gold_num);
			holder.imgv_logo_bg = (ImageView) convertView.findViewById(R.id.imgv_logo_bg);
			holder.tv_add = (TextView) convertView.findViewById(R.id.tv_add);
			holder.image_add = (ImageView) convertView.findViewById(R.id.image_add);
			convertView.setTag(holder);
		} else {
			holder = (holder) convertView.getTag();
		}

		holder.tv_add.setVisibility(View.GONE);
		holder.image_add.setVisibility(View.GONE);
		holder.tv_name.setVisibility(View.VISIBLE);
		holder.tv_gold_num.setVisibility(View.VISIBLE);
		holder.imgv_logo_bg.setVisibility(View.VISIBLE);
		holder.image.setVisibility(View.VISIBLE);
		if(position == rewardUsers.size()){
			holder.imgv_logo_bg.setVisibility(View.GONE);
			holder.image.setVisibility(View.GONE);
			holder.tv_name.setVisibility(View.GONE);
			holder.vip.setVisibility(View.GONE);
			holder.tv_gold_num.setVisibility(View.GONE);
			holder.tv_add.setVisibility(View.VISIBLE);
			holder.image_add.setVisibility(View.VISIBLE);
			holder.image_add.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					if(addRewardClickListener!=null){
						addRewardClickListener.addReward();
					}
				}
			});
		}else{
			final RewardUser rewardUser = rewardUsers.get(position);
			final User user = rewardUser.getUser();
			glideUtils.Glidde_loadCropCircleImage(mContext,user.getLogo(), holder.image);
			holder.tv_name.setText(user.getNick()+"");
			holder.tv_gold_num.setText(rewardUser.getGold()+"");

			if(position==0){
				holder.imgv_logo_bg.setBackgroundResource(R.drawable.n1);
			}else if(position==1){
				holder.imgv_logo_bg.setBackgroundResource(R.drawable.n2);
			}else if(position == 2){
				holder.imgv_logo_bg.setBackgroundResource(R.drawable.n3);
			}else{
				holder.imgv_logo_bg.setBackgroundDrawable(null);
			}

			holder.image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent=new Intent(mContext, UserInfoOtherActivity.class);
					intent.putExtra("user",user);
					mContext.startActivity(intent);
				}
			});
			if(null!=user.getUserAuth()){
				if ("-1".equals(user.getUserAuth().getType())){
					holder.vip.setVisibility(View.GONE);
				}
				if("0".equals(user.getUserAuth().getType())){
					holder.vip.setVisibility(View.VISIBLE);
					holder.vip.setImageResource(R.drawable.v_huan);
				}else if("1".equals(user.getUserAuth().getType())){
					holder.vip.setVisibility(View.VISIBLE);
					holder.vip.setImageResource(R.drawable.v_lan);
				}
			}else{
				holder.vip.setVisibility(View.GONE);
			}
		}


		return convertView;
	}

	class holder {
		public ImageView image,vip,imgv_logo_bg,image_add;
		public TextView tv_name,tv_gold_num,tv_add;
	}

	/**
	 *
	 */
	public interface onAddRewardClickListener {
		public void addReward();

	}
}
